package com.github.iot.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;

import com.github.iot.R;
import com.github.iot.dao.CommonDaoUtils;
import com.github.iot.dao.DaoUtilsStore;
import com.github.iot.entity.DoorCommand;
import com.github.iot.utils.BarUtils;
import com.github.iot.utils.ConvertHexUtils;
import com.github.iot.utils.KeyboardUtils;
import com.github.iot.utils.ToastUtils;
import com.github.iot.view.RxTitle;
import com.github.iot.view.dialogs.fragment.SimpleDialogFragment;
import com.github.iot.view.dialogs.iface.ISimpleDialogListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 柜门指令Activity
 *
 * @Author: 梁声洪
 * @Date: 2019/12/06 9:14
 * @Version: v1.0.0
 */
public class DoorCommandActivity extends AppCompatActivity implements ISimpleDialogListener {

    @BindView(R.id.rx_title)
    RxTitle mTitle;

    @BindView(R.id.et_down_close)
    AppCompatEditText mDownClose;

    @BindView(R.id.et_up_close)
    AppCompatEditText mUpClose;

    @BindView(R.id.et_down_open)
    AppCompatEditText mDownOpen;

    @BindView(R.id.et_up_open)
    AppCompatEditText mUpOpen;

    private static DoorCommand command;
    private static final int REQUEST_SIMPLE_DIALOG = 11;
    private static CommonDaoUtils<DoorCommand> doorCommandDaoUtils = DaoUtilsStore.getInstance().getDoorCommandDaoUtils();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtils.setStatusBarColor(this, R.color.colorPrimaryDark);
        setContentView(R.layout.activity_door_command);

        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initView() {
        mTitle.setLeftFinish(this);
    }

    private void initData() {
        List<DoorCommand> list = doorCommandDaoUtils.queryAll();
        if (list.size() > 0) {
            command = list.get(0);

            mDownClose.setText(ConvertHexUtils.byte2HexStr(command.getDownClose()));
            mUpClose.setText(ConvertHexUtils.byte2HexStr(command.getUpClose()));
            mDownOpen.setText(ConvertHexUtils.byte2HexStr(command.getDownOpen()));
            mUpOpen.setText(ConvertHexUtils.byte2HexStr(command.getUpOpen()));
        }
    }

    @OnClick({R.id.btn_door_command})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_door_command:
                KeyboardUtils.hideSoftInput(DoorCommandActivity.this);
                SimpleDialogFragment.createBuilder(DoorCommandActivity.this, getSupportFragmentManager())
                        .setTitle("系统提示")
                        .setMessage("您确定要操作吗？")
                        .setPositiveButtonText("确定")
                        .setNegativeButtonText("取消")
                        .setRequestCode(REQUEST_SIMPLE_DIALOG)
                        .setCancelable(false)
                        .show();
                break;
            default:

                break;
        }
    }

    private void doorCommand() {
        String downClose = mDownClose.getText().toString();
        String upClose = mUpClose.getText().toString();
        String downOpen = mDownOpen.getText().toString();
        String upOpen = mUpOpen.getText().toString();

        if (TextUtils.isEmpty(downClose)) {
            ToastUtils.warning("关闭按钮按下指令不能为空");
            return;
        }

        if (TextUtils.isEmpty(upClose)) {
            ToastUtils.warning("关闭按钮松开指令不能为空");
            return;
        }

        if (TextUtils.isEmpty(downOpen)) {
            ToastUtils.warning("打开按钮按下指令不能为空");
            return;
        }

        if (TextUtils.isEmpty(upOpen)) {
            ToastUtils.warning("打开按钮松开指令不能为空");
            return;
        }

        downClose = downClose.trim().replaceAll(" ", "");
        upClose = upClose.trim().replaceAll(" ", "");
        downOpen = downOpen.trim().replaceAll(" ", "");
        upOpen = upOpen.trim().replaceAll(" ", "");

        if (null != command) {
            command.setDownClose(ConvertHexUtils.hexStrToByteArray(downClose));
            command.setUpClose(ConvertHexUtils.hexStrToByteArray(upClose));
            command.setDownOpen(ConvertHexUtils.hexStrToByteArray(downOpen));
            command.setUpOpen(ConvertHexUtils.hexStrToByteArray(upOpen));
            boolean flag = doorCommandDaoUtils.update(command);
            if (flag) {
                ToastUtils.success("操作成功");
            } else {
                ToastUtils.error("操作失败");
            }
        } else {
            command = new DoorCommand();
            command.setDownClose(ConvertHexUtils.hexStrToByteArray(downClose));
            command.setUpClose(ConvertHexUtils.hexStrToByteArray(upClose));
            command.setDownOpen(ConvertHexUtils.hexStrToByteArray(downOpen));
            command.setUpOpen(ConvertHexUtils.hexStrToByteArray(upOpen));
            long i = doorCommandDaoUtils.insert(command);
            if (i > 0) {
                ToastUtils.success("操作成功");
            } else {
                ToastUtils.error("操作失败");
            }
        }
    }

    @Override
    public void onNegativeButtonClicked(int requestCode) {

    }

    @Override
    public void onNeutralButtonClicked(int requestCode) {

    }

    @Override
    public void onPositiveButtonClicked(int requestCode) {
        if (requestCode == REQUEST_SIMPLE_DIALOG) {
            doorCommand();
        }
    }
}